package com.wrb.pwd.component;

import com.wrb.pwd.enums.PasswordTypeEnum;
import com.wrb.pwd.model.User;
import com.wrb.pwd.service.UserService;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.regex.Pattern;


/**
 * @author wrb
 * @date 2019/10/16 14:28
 */
public class UserPasswordGeneratorDialog extends JDialog {

	private String password;

	private int passwordLength;

	private int seed;

	private java.util.List<PasswordTypeEnum> passwordTypeList;

	public String getPassword() {
		return password;
	}

	public int getPasswordLength() {
		return passwordLength;
	}

	public int getSeed() {
		return seed;
	}

	public List<PasswordTypeEnum> getPasswordTypeList() {
		return passwordTypeList;
	}

	public UserPasswordGeneratorDialog(Component parentComponent, User user) {
		super((Frame) null, "生成密码", true);

		// 设置对话框的宽高
		this.setSize(400, 300);
		// 设置对话框大小不可改变
		this.setResizable(false);
		// 设置对话框相对显示的位置
		this.setLocationRelativeTo(parentComponent);

		// 创建标签显示消息内容
		JLabel lbPasswordType = new JLabel("生成的密码类型：");
		JCheckBox chkboxNumber = new JCheckBox("数   字", user.getPasswordTypeList().contains(PasswordTypeEnum.NUMBER));
		JCheckBox chkboxUpper = new JCheckBox("大写字母", user.getPasswordTypeList().contains(PasswordTypeEnum.UPPER_CASE_LETTER));
		JCheckBox chkboxLower = new JCheckBox("小写字母", user.getPasswordTypeList().contains(PasswordTypeEnum.LOWER_CASE_LETTER));
		JCheckBox chkboxSymbol = new JCheckBox("特殊符号", user.getPasswordTypeList().contains(PasswordTypeEnum.SPACIAL_SYMBOL));
		JLabel lbPasswordLength = new JLabel("密码长度：");
		JTextField txtPasswordLength = new JTextField(20);
		if (user.getPasswordLength() != 0) {
			txtPasswordLength.setText(String.valueOf(user.getPasswordLength()));
		}
		JLabel lbPasswordSeed = new JLabel("密码种子：");
		JTextField txtPasswordSeed = new JTextField(20);
		if (user.getSeed() != 0) {
			txtPasswordSeed.setText(String.valueOf(user.getSeed()));
		}else {
			txtPasswordSeed.setText(String.valueOf(new Random().nextInt(99999) + 1));
		}

		//显示信息的标签
		JLabel lbIMsg = new JLabel("<html><body>1. 勾选需要的密码类型，至少勾选一个<br>2. 密码长度为整数，范围 4 - 99<br>3. 密码种子，不同种子生成不同密码，种子为整数，范围1 - 99999<body></html>");

		//设置标签的文字是红色
		lbIMsg.setForeground(Color.GREEN);

		// 创建一个按钮用于关闭对话框
		JButton okBtn = new JButton("生成");
		JButton cancelBtn = new JButton("取消");

		okBtn.addActionListener(e -> {
			// 校验
			java.util.List<PasswordTypeEnum> list = new ArrayList<>();
			if (chkboxNumber.isSelected()) {
				list.add(PasswordTypeEnum.NUMBER);
			}
			if (chkboxUpper.isSelected()) {
				list.add(PasswordTypeEnum.UPPER_CASE_LETTER);
			}
			if (chkboxLower.isSelected()) {
				list.add(PasswordTypeEnum.LOWER_CASE_LETTER);
			}
			if (chkboxSymbol.isSelected()) {
				list.add(PasswordTypeEnum.SPACIAL_SYMBOL);
			}
			if (list.size() == 0) {
				JOptionPane.showMessageDialog(this, "请至少勾选一个密码类型", "出错了", JOptionPane.INFORMATION_MESSAGE);
				return;
			}
			String lengthText = txtPasswordLength.getText().trim();
			int passwordLengthInput;
			if (lengthText.length() > 0 && lengthText.length() < 3 && lengthText.charAt(0) != '-') {
				Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
				if (pattern.matcher(lengthText).matches()) {
					passwordLengthInput = Integer.valueOf(lengthText);
				} else {
					JOptionPane.showMessageDialog(this, "密码长度请输入 4 - 99 的整数", "出错了", JOptionPane.INFORMATION_MESSAGE);
					return;
				}
			} else {
				JOptionPane.showMessageDialog(this, "密码长度请输入 4 - 99 的整数", "出错了", JOptionPane.INFORMATION_MESSAGE);
				return;
			}
			String seedText = txtPasswordSeed.getText().trim();
			int seedInput;
			if (seedText.length() > 0 && seedText.length() < 6 && seedText.charAt(0) != '-') {
				Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
				if (pattern.matcher(seedText).matches()) {
					seedInput = Integer.valueOf(seedText);
					if (seedInput == 0) {
						JOptionPane.showMessageDialog(this, "密码种子请输入 1 - 99999 的整数", "出错了", JOptionPane.INFORMATION_MESSAGE);
						return;
					}
				} else {
					JOptionPane.showMessageDialog(this, "密码种子请输入 1 - 99999 的整数", "出错了", JOptionPane.INFORMATION_MESSAGE);
					return;
				}
			} else {
				JOptionPane.showMessageDialog(this, "密码种子请输入 1 - 99999 的整数", "出错了", JOptionPane.INFORMATION_MESSAGE);
				return;
			}
			String password = UserService.getInstance().generatePassword(user.getPlatform(), passwordLengthInput, seedInput, list);
			if (password.length() == passwordLengthInput) {
				// 为空则为生成 不为空为更新密码
				if (user.getId() == null) {
					JOptionPane.showMessageDialog(this, "成功", "结果", JOptionPane.INFORMATION_MESSAGE);
				}else {
					User updateUser = new User();
					updateUser.setId(user.getId());
					Date now = new Date();
					updateUser.setUpdateTime(now);
					updateUser.setPasswordTypeList(list);
					updateUser.setPassword(password);
					updateUser.setPasswordLength(passwordLengthInput);
					updateUser.setSeed(seedInput);
					boolean isSuccess = UserService.getInstance().updateUser(updateUser);
					if (isSuccess) {
						JOptionPane.showMessageDialog(this, "修改成功", "修改结果", JOptionPane.INFORMATION_MESSAGE);
					} else {
						JOptionPane.showMessageDialog(this, "程序出错了", "修改结果", JOptionPane.INFORMATION_MESSAGE);
						return;
					}
				}
			} else {
				JOptionPane.showMessageDialog(this, "程序出错了", "结果", JOptionPane.INFORMATION_MESSAGE);
				return;
			}
			this.password = password;
			this.passwordLength = passwordLengthInput;
			this.seed = seedInput;
			this.passwordTypeList = list;
			this.dispose();
		});
		cancelBtn.addActionListener(e -> this.dispose());

		lbPasswordType.setBounds(20, 20, 200, 25);
		chkboxNumber.setBounds(20, 50, 80, 25);
		chkboxUpper.setBounds(110, 50, 80, 25);
		chkboxLower.setBounds(200, 50, 80, 25);
		chkboxSymbol.setBounds(290, 50, 80, 25);
		lbPasswordLength.setBounds(20, 80, 80, 25);
		txtPasswordLength.setBounds(120, 80, 120, 25);
		lbPasswordSeed.setBounds(20, 110, 120, 25);
		txtPasswordSeed.setBounds(120, 110, 120, 25);
		lbIMsg.setBounds(20, 140, 280, 75);
		okBtn.setBounds(120, 230, 60, 25);
		cancelBtn.setBounds(220, 230, 60, 25);

		// 创建对话框的内容面板, 在面板内可以根据自己的需要添加任何组件并做任意是布局
		JPanel panel = new JPanel(null);

		// 添加组件到面板
		panel.add(lbPasswordType);
		panel.add(chkboxNumber);
		panel.add(chkboxUpper);
		panel.add(chkboxLower);
		panel.add(chkboxSymbol);
		panel.add(lbPasswordLength);
		panel.add(txtPasswordLength);
		panel.add(lbPasswordSeed);
		panel.add(txtPasswordSeed);
		panel.add(lbIMsg);
		panel.add(okBtn);
		panel.add(cancelBtn);

		// 设置对话框的内容面板
		this.setContentPane(panel);
		//设置模式 dialog关闭后才能获取state的值
		this.setModal(true);
		// 显示对话框
		this.setVisible(true);
	}
}
